from selenium import webdriver
import time

def writetxt(msg, type):
    # print(request.META)
    if type == 1:
        try:
            with open('电影页数.txt'+str(msg), 'a+', encoding='utf-8') as f:
                f.write(msg + '\n')
        except:
            pass

def gettimestamp():
    t = time.time()
    # print(t)  # 原始时间数据
    print(int(t))  # 秒级时间戳
    print(int(round(t * 1000)))  # 毫秒级时间戳
    # nowTime = lambda: int(round(t * 1000))  # 毫秒级时间戳步骤1，基于lambda
    # print(nowTime())  # 毫秒级时间戳步骤2，基于lambda
    return int(t)

# 折中方案，参数按如下方式组织，也是模拟multipart/form-data的核心

options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
web = input("网站域名类似：http://xhp.ldbyy.cn，按回车继续")
a = input("请输入开始视频编号，输入后按回车继续")
b = input("请输入结束视频编号，输入后按回车继续")
c = input("请输入话术，输入后按回车继续")
for i in range(int(b)):
    if i < int(a):
        continue
    #
    # url = f'{web}/index.php/vod/play/id/2/sid/1/nid/1.html?id={i}'
    try:
        url = f'{web}/index.php/vod/play/id/{i}/sid/1/nid/1.html'
        print(url)
        driver.get(url=url)
        content = driver.page_source
    except:
        driver.close()
    # print(content)
    try:
        driver.implicitly_wait(1)
        # driver.find_element_by_xpath('//*[@class="close"]/img').click()
        driver.find_element_by_xpath('//*[@class="fed-padding fed-col-xs4"]/input').send_keys(c)
        driver.find_element_by_xpath('//*[@class="fed-padding fed-col-xs2"]/a').click()
        writetxt(i,1)
        driver.delete_all_cookies()
    except:
        continue
driver.close()